// let loginInfo = {}
// export const baseUrl = 'https://neat-lamprey-strongly.ngrok-free.app/frieneed-mini-program'
export const baseUrl = 'https://api.mugua.co/frieneed-mini-program'
// 从缓存中获取用户登录信息
import tran from './language'
// 获取语言
// const language = tran.getLanguage()
let language = String(wx.getAppBaseInfo().language)
// 因为传参是中划线，所以，就将下划线转成中划线
let langList = language.split('_')
language = langList.join('-')
// getApp().getLogin()
export const requestInt = (params) => {
  let loginInfo = wx.getStorageSync('loginInfo')
  const { url, method, contentType, data, isHeaderData, isData, headers, isCurrentUrl, hideLoadingFlag } = params;
  // 返回一个promise对象
  return new Promise((resolve, reject) => {
    // 判断是否隐藏加载中的loading
    if (!hideLoadingFlag) {
      wx.showLoading({
        title: '加载中...',
        mask: true
      })
    }
    wx.request({
      url: isCurrentUrl ? url : `${baseUrl}${url}`, //仅为示例，并非真实的接口地址
      data: data,
      header: {
        'content-type': contentType || 'application/json',
        'frieneed-mini-program-satoken': loginInfo.token,
        "Accept-Language": language,
        ...headers
      },
      method: method || 'POST',
      success: (res) => {
        console.log(res, 'dededede');
        wx.hideLoading()
        // 请求成功，就将成功的数据返回出去
        const { data, header } = res
        // 成功
        if (data.code == 200) {
          // 判断是否将header返回出去
          if (isHeaderData) {
            const resObj = { data, header }
            resolve(resObj)
          } else {
            resolve(data.data)
          }
        } else if (data.code == 5001) { // 没登录，或者登录失效
          resolve(data)
        } else if (data.code == 5003) { // token被顶下线
          wx.clearStorage()
          getApp().globalData.token = ''
          getApp().globalData.userId = ''
          wx.switchTab({
            url: '/pages/home/index'
          })
        } else if (data.code == 7000) { // 请完善个人信息
          resolve(data)
        } else if (data.code == 3001 || data.code == 3003 || data.code == 3004) { // 账号不存在，账号被禁用，账号被注销
          wx.clearStorage()
          getApp().globalData.token = ''
          getApp().globalData.userId = ''
          wx.switchTab({
            url: '/pages/home/index'
          })
          resolve(data)
        } else {
          getApp().loginMobileFlase()
          if (isData) {
            resolve(data)
          } else {
            setTimeout(() => {
              wx.showToast({
                title: data.message || '网络出错',
                icon: 'none',
                duration: 3000
              })
            }, 100)
          }
        }
      },
      fail: (err) => {
        wx.hideLoading()
        console.log('接口报错', err);
        reject(err)
      },
    })
  })
}

/**
 * 上传文件的请求封装
 * @param {*} params 
 */
export const requestFileInt = (params) => {
  console.log(params);
  let loginInfo = wx.getStorageSync('loginInfo')
  const { url, method, contentType, data, isHeaderData, isData, headers } = params;
  // 返回一个promise对象
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title: '加载中...',
      mask: true
    })
    wx.request({
      url: url, //仅为示例，并非真实的接口地址
      data: data,
      header: {
        // 'content-type': contentType || 'application/json',
        'content-type': 'image/png'
        // 'frieneed-mini-program-satoken': loginInfo.token,
        // "Accept-Language": language
      },
      method: method || 'POST',
      success: (res) => {
        wx.hideLoading()
        // 成功
        if (res.statusCode == 200) {
          resolve(res)
        } else {
          setTimeout(() => {
            wx.showToast({
              title: data.message || '网络出错',
              icon: 'none',
              duration: 3000
            })
          }, 100)
        }

      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}